package org.tgcloud.group.service.manager;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.tgcloud.group.core.APPResultMessage;
import org.tgcloud.group.core.RedisUtil;
import org.tgcloud.group.core.ResultMessage;
import org.tgcloud.group.core.StringUtil;
import org.tgcloud.group.entity.MjMediumOffline;
import org.tgcloud.group.entity.MjMediumOnline;
import org.tgcloud.group.entity.SysLabelValue;
import org.tgcloud.group.repository.MjMediumOfflineMapper;
import org.tgcloud.group.repository.MjMediumOnlineMapper;
import org.tgcloud.group.repository.SysLabelValueMapper;
import org.tgcloud.group.web.controller.dto.medium.*;

import java.math.BigDecimal;
import java.util.*;

/**
 * @author: zhy
 * @create: 2023-11-10 10:24
 * @description:
 */
@Service
public class MediumManageService {

    private static String mediumOfflineCascaderRedisKey = "mediumOfflineCascader";

    @Autowired
    private MjMediumOnlineMapper mediumOnlineMapper;

    @Autowired
    private MjMediumOfflineMapper mediumOfflineMapper;

    @Autowired
    private SysLabelValueMapper sysLabelValueMapper;

    public APPResultMessage getMediumOnlineList(Integer pageNum, Integer pageSize, String keyword, Integer userType, String roleIds) {
        boolean isBusiness = false;
        // 判断是否是商务账户
        if (StringUtil.isNotBlank(roleIds) && (StringUtil.containsString(roleIds.split(","),"40") || StringUtil.containsString(roleIds.split(","),"55")) ){
            isBusiness = true;
        }
        if (pageNum != null && pageSize != null){
            PageHelper.startPage(pageNum, pageSize);
            List<MediumOnlineDTO> dtos = mediumOnlineMapper.selectManageList(keyword,userType, isBusiness);
            PageInfo<MediumOnlineDTO> tables = new PageInfo<>(dtos);
            ResultMessage result = ResultMessage.builder().setList(tables.getList()).setAllPageNumber(tables.getPages()).setCount(tables.getTotal());
            APPResultMessage<Object, MediumOnlineDTO> message = APPResultMessage.getSuccessMessage();
            message.setResult(result);
            return message;
        }else {
            List<MediumOnlineDTO> dtos = mediumOnlineMapper.selectManageList(keyword,userType, isBusiness);
            ResultMessage result = ResultMessage.builder().setList(dtos);
            APPResultMessage<Object, MediumOnlineDTO> message = APPResultMessage.getSuccessMessage();
            message.setResult(result);
            return message;
        }
    }

    public APPResultMessage getMediumOfflineList(Integer pageNum, Integer pageSize, String keyword,
                                                 Integer offlineType, Short status, Integer excelShowTitleId, String roleIds) {
        boolean isBusiness = false;
        // 判断是否是商务账户
        if (StringUtil.isNotBlank(roleIds) && (StringUtil.containsString(roleIds.split(","),"40") || StringUtil.containsString(roleIds.split(","),"55")) ){
            isBusiness = true;
        }
        if (pageNum != null && pageSize != null){
            PageHelper.startPage(pageNum, pageSize);
            List<MediumOfflineDTO> dtos = mediumOfflineMapper.selectManageList(keyword,offlineType, status, excelShowTitleId, isBusiness);
            PageInfo<MediumOfflineDTO> tables = new PageInfo<>(dtos);
            ResultMessage result = ResultMessage.builder().setList(tables.getList()).setAllPageNumber(tables.getPages()).setCount(tables.getTotal());
            APPResultMessage<Object, MediumOfflineDTO> message = APPResultMessage.getSuccessMessage();
            message.setResult(result);
            return message;
        }else {
            List<MediumOfflineDTO> dtos = mediumOfflineMapper.selectManageList(keyword,offlineType, status, excelShowTitleId, isBusiness);
            ResultMessage result = ResultMessage.builder().setList(dtos);
            APPResultMessage<Object, MediumOfflineDTO> message = APPResultMessage.getSuccessMessage();
            message.setResult(result);
            return message;
        }
    }

    public APPResultMessage doAddOnlineMedium(Integer userType, String plateName, String fans, BigDecimal cost,
                                              BigDecimal goodPrice, String remark, String createType, String steadyContent,
                                              String pic, String oneInteractionNum, String oneExposureNum) {
        MjMediumOnline mjMediumOnline = mediumOnlineMapper.selectHasPlateNameByPlateName(plateName);
        if (mjMediumOnline != null){
            return new APPResultMessage(6001, "该平台名已被添加，请重新填写");
        }
        MjMediumOnline mediumOnline = new MjMediumOnline();
        mediumOnline.setMediumType((short) 2);
        mediumOnline.setUserType(userType);
        mediumOnline.setPlateName(plateName);
        mediumOnline.setFans(fans);
        mediumOnline.setCost(cost);
        mediumOnline.setGoodPrice(goodPrice);
        mediumOnline.setCreateTime(new Date());
        mediumOnline.setStatus((short) 1);
        mediumOnline.setRemark(remark);
        mediumOnline.setCreateType(createType);
        mediumOnline.setSteadyContent(steadyContent);
        mediumOnline.setPic(pic);
        mediumOnline.setOneInteractionNum(oneInteractionNum);
        mediumOnline.setOneExposureNum(oneExposureNum);
        mediumOnlineMapper.insertSelective(mediumOnline);
        return APPResultMessage.getSuccessMessage();
    }

    public APPResultMessage doAddOfflineMedium(MjMediumOffline offline) {
        MjMediumOffline mediumOffline = new MjMediumOffline();
        BeanUtils.copyProperties(offline, mediumOffline);
        mediumOffline.setId(null);
        mediumOffline.setCreateTime(new Date());
        mediumOffline.setUpdateTime(new Date());
        if (mediumOffline.getOfflineType() == null){
            mediumOffline.setOfflineType(mediumOffline.getExcelShowTitleId());
        }
        mediumOfflineMapper.insertSelective(mediumOffline);
        RedisUtil.deleteObject(mediumOfflineCascaderRedisKey);
        return APPResultMessage.getSuccessMessage();
    }

    public APPResultMessage doUpdateOnlineMedium(Integer userType, String plateName, String fans, BigDecimal cost, BigDecimal goodPrice, int onlineMediumId,
                                                 String remark, String createType, String steadyContent, String pic,
                                                 String oneInteractionNum, String oneExposureNum) {
        MjMediumOnline mjMediumOnline = mediumOnlineMapper.selectHasPlateNameByPlateName(plateName);
        if (mjMediumOnline != null && mjMediumOnline.getId() != onlineMediumId){
            return new APPResultMessage(6001, "该平台名已被添加，请重新填写");
        }
        MjMediumOnline mediumOnline = new MjMediumOnline();
        mediumOnline.setMediumType((short) 2);
        mediumOnline.setUserType(userType);
        mediumOnline.setPlateName(plateName);
        mediumOnline.setFans(fans);
        mediumOnline.setCost(cost);
        mediumOnline.setGoodPrice(goodPrice);
        mediumOnline.setStatus((short) 1);
        mediumOnline.setId(onlineMediumId);
        mediumOnline.setRemark(remark);
        mediumOnline.setCreateType(createType);
        mediumOnline.setSteadyContent(steadyContent);
        mediumOnline.setPic(pic);
        mediumOnline.setOneInteractionNum(oneInteractionNum);
        mediumOnline.setOneExposureNum(oneExposureNum);
        mediumOnlineMapper.updateByPrimaryKeySelective(mediumOnline);
        return APPResultMessage.getSuccessMessage();
    }

    public APPResultMessage doUpdateOfflineMedium(MjMediumOffline offline) {
        MjMediumOffline mediumOffline = new MjMediumOffline();
        BeanUtils.copyProperties(offline, mediumOffline);
        mediumOffline.setUpdateTime(new Date());
        if (mediumOffline.getOfflineType() == null){
            mediumOffline.setOfflineType(mediumOffline.getExcelShowTitleId());
        }
        mediumOfflineMapper.updateByPrimaryKeySelective(mediumOffline);
        RedisUtil.deleteObject(mediumOfflineCascaderRedisKey);
        return APPResultMessage.getSuccessMessage();
    }


    public APPResultMessage doDelOnlineMedium(int id) {
        mediumOnlineMapper.updateStatusById(id, (short) 10);
        return APPResultMessage.getSuccessMessage();
    }

    public APPResultMessage doDelOfflineMedium(int id) {
        mediumOfflineMapper.updateStatusById(id, (short) 10);
        RedisUtil.deleteObject(mediumOfflineCascaderRedisKey);
        return APPResultMessage.getSuccessMessage();
    }

    public APPResultMessage getMediumOfflineCascader() {
        List<MediumOfflineCascaderDTO> list = new ArrayList<>();
        if (RedisUtil.existKey(mediumOfflineCascaderRedisKey)){
            list = RedisUtil.getCacheList(mediumOfflineCascaderRedisKey);
        } else {
            List<MjMediumOffline> mediumOfflines = mediumOfflineMapper.selectCascader();
            Map<Integer, List<MjMediumOffline>> map = new HashMap<>();
            for (MjMediumOffline offline : mediumOfflines){
                if(!map.containsKey(offline.getOfflineType())){
                    List<MjMediumOffline> offlineList = new ArrayList<>();
                    offlineList.add(offline);
                    map.put(offline.getOfflineType(), offlineList);
                } else {
                    List<MjMediumOffline> offlineList = map.get(offline.getOfflineType());
                    offlineList.add(offline);
                    map.put(offline.getOfflineType(), offlineList);
                }
            }
            for (Integer key : map.keySet()){
                MediumOfflineCascaderDTO dto = new MediumOfflineCascaderDTO();
                SysLabelValue sysLabelValue = sysLabelValueMapper.selectByPrimaryKey(key);
                dto.setLabel(sysLabelValue.getSysValueName());
                dto.setValue(sysLabelValue.getId());
                List<MediumOfflineCascaderDTO> childrenList = new ArrayList<>();
                List<MjMediumOffline> offlineList = map.get(key);
                for (MjMediumOffline offline : offlineList){
                    MediumOfflineCascaderDTO childDto = new MediumOfflineCascaderDTO();
                    childDto.setLabel(offline.getName());
                    childDto.setValue(offline.getId());
                    childrenList.add(childDto);
                }
                dto.setChildren(childrenList);
                list.add(dto);
            }
            RedisUtil.setCacheList(mediumOfflineCascaderRedisKey, list);
        }
        APPResultMessage message = APPResultMessage.getSuccessMessage();
        ResultMessage resultMessage = ResultMessage.builder().setList(list);
        message.setResult(resultMessage);
        return message;
    }
}
